﻿using QFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace ProjectSurvivor
{
    public class CoinUpgradeSystem : AbstractSystem,ICanSave
    {

        public static EasyEvent OnCoinUpgradeSystemChanged = new EasyEvent();


        public List<CoinUpgradeItem> Items = new List<CoinUpgradeItem>();


        public CoinUpgradeItem Add(CoinUpgradeItem item)
        {
            Items.Add(item);
            return item;
        }

        protected override void OnInit()
        {
         var coinPercentLv1=  Add( new CoinUpgradeItem()
                .WithKey("coin_percent_lv1")
                .WithDescription("金币掉落概率提升 Lv1")
                .WithPrice(5)
                .OnUpgrade((item) =>
                {
                    Global.CoinPercent.Value += 0.1f;
                    Global.Coin.Value -= item.Price;
                }));

          var coinPercentLv2= Add(new CoinUpgradeItem()
            .WithKey("coin_percent_lv2")
            .WithDescription("金币掉落概率提升 Lv2")
            .WithPrice(7)
            .Condition((_)=>coinPercentLv1.UpgradeFinish)
            .OnUpgrade((item) =>
            {
                Global.CoinPercent.Value += 0.1f;
                Global.Coin.Value -= item.Price;
            }));

            coinPercentLv1.OnChanged.Register(() =>
            {
                coinPercentLv2.OnChanged.Trigger();
            });

           var coinpercentLv3=  Add(new CoinUpgradeItem()
            .WithKey("coin_percent_lv3")
            .WithDescription("金币掉落概率提升 Lv3")
            .WithPrice(10)
            .Condition((_) => coinPercentLv2.UpgradeFinish)
            .OnUpgrade((item) =>
            {
                Global.CoinPercent.Value += 0.1f;
                Global.Coin.Value -= item.Price;
            }));

            coinPercentLv2.OnChanged.Register(() =>
            {
                coinpercentLv3.OnChanged.Trigger();
            });


            Items.Add(new CoinUpgradeItem()
               .WithKey("exp_percent")
               .WithDescription("经验值掉落概率提升")
               .WithPrice(5)
               .OnUpgrade((item) =>
               {
                   Global.ExpPercent.Value += 0.1f;
                   Global.Coin.Value -= item.Price;
               }));

            Items.Add(new CoinUpgradeItem()
               .WithKey("max_hp")
               .WithDescription("主角的最大血量+1")
               .WithPrice(30)
               .OnUpgrade((item) =>
               {
                   Global.MaxHP.Value++;
                   Global.Coin.Value -= item.Price;
               }));

            Load();

            OnCoinUpgradeSystemChanged.Register(() =>
            {
                Save();
            });
        }


        public void Say()
        {
            Debug.Log("Hello CoinUpgradeSystem");
        }

        public void Save()
        {
            var saveSystem=this.GetSystem<SaveSystem>();
            foreach (var coinUpgradeItem in Items)
            {
                saveSystem.SaveBool(coinUpgradeItem.key, coinUpgradeItem.UpgradeFinish);
            }
        }

        public void Load()
        {
            var saveSystem = this.GetSystem<SaveSystem>();
            foreach (var coinUpgradeItem in Items)
            {
              coinUpgradeItem.UpgradeFinish=  saveSystem.LoadBool(coinUpgradeItem.key,false);
            }
        }
    }
}
